import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ConcurrentHashMap;

/*This class listens for clients. If a client contacts the server it spawns a thread to handle it*/

public class Receiver extends Thread{

	public ServerSocket socket;
	public FingerTable ft;
	public ConcurrentHashMap<Integer,String> keys; //stores key-value pairs
	
	public Receiver(MachineData m, FingerTable ft, ConcurrentHashMap<Integer,String> keys) {
		
        try {
            socket = new ServerSocket(m.port);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
       
        this.ft = ft;
        this.keys = keys;
	}	
	
	public void run() {
		
		
        while (true) {       
        	try {
				(new ReceiverThread(socket.accept(), ft, keys)).start(); //start a thread
				System.out.println("Thread Created");
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
		
	}
	
	
}
